package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.dao.CommandRecordMapper;
import com.wtwd.campus.dao.DeviceInfoMapper;
import com.wtwd.campus.dao.DeviceSettingMapper;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.utils.ReceiverUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author mjy
 * @date 2021/07/01
 * @description
 */
@Service
@MsgType(value = Constants.SET_DEFAULT_CONTROL_CENTER)
public class ReceiveDefaultServerSettingHandler implements IReceiverHandler {

    @Autowired
    private CommandRecordMapper commandRecordMapper;
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceSettingMapper deviceSettingMapper;
    @Autowired
    private DeviceLogService deviceLogService;
    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqIpAndPortResult mqIpAndPortResult = (MqIpAndPortResult) mqMessage.getObject();
        //根据流水号，imei号，和指令类型找到唯一的一条指令记录
        CommandRecord commandRecord = commandRecordMapper.getCommandRecordByCondition2(mqIpAndPortResult.getImei(), mqIpAndPortResult.getCommandNumber(), Constants.SET_DEFAULT_CONTROL_CENTER);
        if (commandRecord != null) {
            //鉴别是否是这条指令，不是这条指令不处理
            if (commandRecord.getCommandNumber().equals(mqIpAndPortResult.getCommandNumber())) {
                DeviceInfo deviceInfo = deviceInfoMapper.getDeviceInfoByImei(mqIpAndPortResult.getImei());
                if (deviceInfo != null) {
                    //收到命令回复成功后将ip地址和端口号,重连次数，以及状态更新
                    DeviceSetting deviceSetting = deviceSettingMapper.getDeviceSettingByDeviceId(deviceInfo.getDeviceId());
                    if (deviceSetting != null) {
                        String deviceTime = mqIpAndPortResult.getDeviceTime();
                        if (StringUtils.isBlank(deviceTime)) {
                            Long deviceTime1 = Long.valueOf(deviceTime);
                            deviceSetting.setReconnectTime(deviceTime1);
                        }
                        deviceSetting.setPort(mqIpAndPortResult.getPort());
                        deviceSetting.setIpAddress(mqIpAndPortResult.getIp());
                        deviceSettingMapper.modifyDeviceSetting(deviceSetting);
                    }
                    //将指令状态改为完成
                    commandRecordMapper.completeCommand(commandRecord.getId(), System.currentTimeMillis());
                    //如果定位数据不为空的话
                    if (mqIpAndPortResult.getMqLocationData() != null) {
                        if (mqIpAndPortResult.getMqLocationData().getLatitude() != 0 && mqIpAndPortResult.getMqLocationData().getLongitude() != 0) {
                            //进行定位数据存储以及围栏告警记录的存储
                            //处理指令回复的时间
                            String deviceDate = mqIpAndPortResult.getDeviceDate();
                            String deviceTime = mqIpAndPortResult.getDeviceTime();
                            Long time = getTime(deviceDate, deviceTime);
                            deviceInfo = receiverUtils.dealLocation(mqIpAndPortResult.getMqLocationData(), time, deviceInfo, 1);
                        }
                    }
                    deviceInfo.setLastUpTime(System.currentTimeMillis());
                    deviceInfoMapper.updateDeviceInfo(deviceInfo);
                    //删除缓存中的指令
                    String delKey = Constants.SET_DEFAULT_CONTROL_CENTER.concat("-").concat(mqIpAndPortResult.getCommandNumber());
                    receiverUtils.deleteCommandInRedis(mqIpAndPortResult.getImei(), delKey);
                    //存储设备接收日志
                    DeviceLog deviceLog = new DeviceLog();
                    deviceLog.setSendUser(Constants.DEVICE_USER);
                    deviceLog.setImei(mqIpAndPortResult.getImei());
                    deviceLog.setBigType(Constants.MESS_TYPE);
                    deviceLog.setType(Constants.SET_DEFAULT_CONTROL_CENTER);
                    JSONObject contant = new JSONObject();
                    contant.put("设备号", mqIpAndPortResult.getImei());
                    contant.put("ip",mqIpAndPortResult.getIp());
                    contant.put("端口",mqIpAndPortResult.getPort());
                    contant.put("时间",mqIpAndPortResult.getDeviceTime());
                    contant.put("经度", mqIpAndPortResult.getMqLocationData().getLongitude());
                    contant.put("纬度", mqIpAndPortResult.getMqLocationData().getLatitude());
                    contant.put("设备状态",mqIpAndPortResult.getDeviceStatus());
                    deviceLog.setCmd(contant.toJSONString());
                    deviceLog.setSendTime(System.currentTimeMillis());
                    deviceLog.setCommandNumber(mqIpAndPortResult.getCommandNumber());
                    deviceLog.setState(1);
                    deviceLogService.addDeviceLog(deviceLog);
                } else {
                    saveUnInStorage(mqIpAndPortResult.getImei());
                    receiverUtils.cloaseChannel(mqIpAndPortResult.getImei());
                }
            }
        }
    }
}
